import sys

from pyx_package.script import BaseBehavior
from pyx_package.ps_paramete import PsParameter

from time import sleep
PYX_MODULE_NAME = "Print"

class ScriptBehavior(BaseBehavior):

    def __init__(self) -> None:
        super().__init__()
        
        self.SetExeMode("function")
        
        self.InitParams()

        self.InitInst()

    def InitParams(self):
        '''
            初始化参数
        '''
        
        #输出前缀
        self.p_front_suff = self.CreateParam("front_suff", "打印前缀", PsParameter.Input, str, "")
        self.p_front_suff.must = False
        self.p_front_suff.keep_value = True
        
        self.p_content = self.CreateParam("content", "打印内容", PsParameter.Input, any)
        self.p_content.keep_value = False

        #打印字体的级别
        self.p_level = self.CreateParam("level", "打印级别", PsParameter.Input, str)
        self.p_level.value_input_mode = PsParameter.ComboBoxMode
        self.p_level.SetOptionitems(['infomation', 'warning', 'error', 'success'])

    def InitInst(self):
        '''
            初始化指令
        '''

        self.RegisterInstruct("print", "在软件输出窗口打印", self.InstPrint, any, str)

    def DoStep(self):
        
        self.InstPrint(self.p_content, self.p_front_suff)

    def InstPrint(self, content:"打印内容", front_suff:"打印前缀" = ""):
        '''
            指令打印方式
        '''
        try:
            #尝试将打印内容转化为字符串
            value = str(content.value)

            front_suff_content = str(front_suff.value)

            self.Debug(front_suff_content + value, self.p_level.value)

        except:
            self.Debug("-", "warning")
